package com.example.chapter02;

import android.content.Context;
import android.content.SharedPreferences;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;

public class User implements Serializable {
    private String name;
    private String pwd;
    private String phone;
    private String email;
    SharedPreferences sharedPreferences;

    FileInputStream fileInputStream=null;
    FileOutputStream fileOutputStream=null;

    private  static  final  String SHARED_NAME="userInfo";

    public  void saveUserDate(Context context) {
        //方式一
        /*sharedPreferences = context.getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("name",name);
        editor.putString("pwd",pwd);
        editor.putString("email",email);
        editor.putString("phone",phone);
        editor.commit();*/


        //文件方式
        String myuserdata;
        myuserdata=name+","+pwd+","+email+","+phone;
        try {
            fileOutputStream =context.openFileOutput(SHARED_NAME,Context.MODE_PRIVATE);
            fileOutputStream.write(myuserdata.getBytes());
            fileOutputStream.flush();
        }catch (Exception e){

        }
        finally {
            if(fileOutputStream!=null){
                try{
                    fileOutputStream.close();
                }catch (Exception e){
                }
            }

        }

    }

    public void getUser(Context context){
        //方法一
        /*sharedPreferences = context.getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
        name=sharedPreferences.getString("name","");
        phone=sharedPreferences.getString("phone","");
        pwd=sharedPreferences.getString("pwd","");
        email=sharedPreferences.getString("email","");*/


        //文件读取
        try {
            fileInputStream=context.openFileInput(SHARED_NAME);
            byte[] buffer=new byte[fileInputStream.available()];
            fileInputStream.read(buffer);
            String userData=new String(buffer);
            String info[]=userData.split(",");
            System.out.println(info[0]);
            name=info[0];
            phone=info[3];
            pwd=info[1];
            email=info[2];


        }catch (Exception e){

        }finally {

        }


    }


    public User() {
    }

    public User(String name, String pwd, String phone, String email) {
        this.name = name;
        this.pwd = pwd;
        this.phone = phone;
        this.email = email;
    }

    public User(String name, String pwd, String phone) {
        this.name = name;
        this.pwd = pwd;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "" + name + ",恭喜你注册成功：" + '\''+
                ", 手机为：'" + phone + '\'' +
                ", 邮箱为：'" + email + '\'' ;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
